/*
EP3D is a real-time 3D planet engine , which in addition to providing 
substandard scene rendering and scene management, of course, it also 
provides some basic class libraries to build the entire virtual 
planet, or even the entire universe.

Copyright (C) 2010  Hongjiang Zhang	(zhjwyat@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef EP3D_VECTOR2_H
#define EP3D_VECTOR2_H
#include "EP3DBase.h"
#include "EP3DMath.h"

namespace EP3D
{
	/************************************************************************/
	/*vector2 template should only be used with float or double types                                                            
	/************************************************************************/
	template <class T>
	class Vector2
	{
	public:
		T x, y;
	public:
		Vector2(void)	
			:x(0.0f), y(0.0f)
		{
		}

		Vector2(const T tX, const T tY)	
			:x(tX), y(tY)
		{
		}

		void Set(const T tX, const T tY)
		{
			x = tX;
			y = tY;
		}

		Vector2(const T* pt[2])
			:x(pt[0]), y(pt[1])
		{
		}

		Vector2(const Vector2<T>& v)	
			:x(v.x), y(v.y)
		{
		}

		// casting
		operator T* ()
		{
			return (T*)&x;
		}

		operator const T* ()
		{
			return (const T*)&x;
		}


		// assignment operators
		Vector2<T>& operator += (const Vector2<T>& v)	
		{ 
			x += v.x; 
			y += v.y; 

			return *this; 
		}

		Vector2<T>& operator -= (const Vector2<T>& v)	
		{ 
			x -= v.x; 
			y -= v.y; 

			return * this; 
		}

		Vector2<T>& operator *= (const Vector2<T>& v)
		{
			x *= v.x;
			y *= v.y;

			return *this;
		}

		Vector2<T>& operator *= (const T t)	
		{ 
			x *= t; 
			y *= t; 

			return *this; 
		}

		Vector2<T>& operator /= (const Vector2<T>& v)
		{
			x /= v.x;
			y /= v.y;

			return *this;
		}

		Vector2<T>& operator /= (const T t)	
		{ 
			T Invt = 1.0f/t;
			x *= Invt; 
			y *= Invt; 

			return *this; 
		}

		// unary operators
		Vector2<T> operator + (void) const	
		{ 
			return *this; 
		}

		Vector2<T> operator - (void) const	
		{ 
			return Vector2<T>(-x, -y); 
		}

		// binary operators
		Vector2<T> operator + (const Vector2<T>& v) const	
		{ 
			return Vector2<T>(x + v.x, y + v.y); 
		}

		Vector2<T> operator - (const Vector2<T>& v) const	
		{ 
			return Vector2<T>(x - v.x, y - v.y); 
		}

		Vector2<T> operator * (const Vector2<T>& v) const
		{
			return Vector2<T>(x * v.x, y * v.y);
		}

		Vector2<T> operator * (const T t) const	
		{
			return Vector2<T>(x * t, y * t); 
		}

		Vector2<T> operator / (const Vector2<T>& v) const
		{
			return Vector2<T>(x / v.x, y / v.y );
		}

		Vector2<T> operator / (const T t) const	
		{ 
			T Invt = 1.0f/t;
			return Vector2<T>(x * Invt, y * Invt); 
		}

		friend Vector2<T> operator * (const T t, const Vector2<T>& v)	
		{ 
			return Vector2<T>(t * v.x, t * v.y); 
		}

		bool operator == (const Vector2<T>& v) const	
		{ 
			return 0 == memcmp(this, &v, sizeof(Vector2<T>)); 
		}

		bool operator != (const Vector2<T>& v) const	
		{ 
			return 0 != memcmp(this, &v, sizeof(Vector2<T>)); 
		}

		bool operator > (const Vector2<T>& v) const
		{
			return x > v.x && y > v.y;
		}

		bool operator < (const Vector2<T>& v) const
		{
			return x < v.x && y < v.y;
		}

		T SquareLength() const
		{ 
			return x * x + y * y; 
		}

		T Length() const
		{ 
			return Math<T>::Sqrt(SquareLength());
		}

		T SquareDistance(const Vector2<T>& v) const
		{
			return (*this - v).SquareLength();
		}

		T Distance(const Vector2<T>& v) const
		{
			return (*this - v).Length();
		}

		Vector2<T> MidPoint(const Vector2<T>& v) const
		{
			return Vector2<T>((*this - v)/2 + v);
		}

		Vector2<T> Average(const Vector2<T>& v) const
		{
			return Vector2<T>((*this + v) / 2);
		}

		// Advanced methods (should only be used with float or double types)
		T DotProduct(const Vector2<T>& v)	
		{ 
			return x * v.x + y * v.y; 
		}

		T CrossProduct(const Vector2<T>& v)	
		{ 
			return x * v.y - y * v.x;
		}

		void Normalize()
		{
			*this /= Length();
		}

		Vector2<T> Reflect(const Vector2<T>& n ) const
		{
			return Vector2<T>(*this - ( 2 * this->DotProduct(n) * n ));
		}

		Vector2<T> Rotate(const T tAngle) const
		{
			T tCos = Math<T>::Cos(tAngle);
			T tSin = Math<T>::Sin(tAngle);

			return Vector2<T>(x * tCos - y * tSin, x * tSin + y * tCos);
		}
	};
	typedef Vector2<f32> Vector2f, Point;
	typedef Vector2<f64> Vector2d;
	typedef Vector2<int> Vector2i;
	typedef Vector2<short> Vector2s;

}

#endif